package StudyCode.summary.BufferAndChannel;

import java.io.OutputStream;
import java.nio.ByteBuffer;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class GetOutputStreamByteBuffer {
	public static void main(String[] args) {
		ByteBuffer buf = ByteBuffer.allocate(10);
		OutputStream os = newOutputStream(buf);
		System.out.println("从 ByteBuffer 获取的 OutputStream:");
		System.out.println(os);
	}

	private static OutputStream newOutputStream(final ByteBuffer buf) {
		return new OutputStream() {
			public synchronized void write(int b) {
				buf.put((byte) b);
			}

			public synchronized void write(byte[] bytes, int off, int len) {
				buf.put(bytes, off, len);
			}
		};
	}
}
